package cn.sciento.starter.sms.support;

import cn.sciento.starter.sms.entity.SmsConfig;
import com.baidubce.auth.BceCredentials;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageV2Request;
import com.baidubce.services.sms.model.SendMessageV2Response;
import org.apache.commons.lang3.StringUtils;

public class BaiduSmsSupporter {
  public static SmsClient smsClient(SmsConfig smsConfig) {
    SmsClientConfiguration config = new SmsClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(smsConfig.getAccessKey(), smsConfig.getAccessKeySecret()));
    if (StringUtils.isNotBlank(smsConfig.getEndPoint())) {
      config.setEndpoint(smsConfig.getEndPoint());
    }
    return new SmsClient(config);
  }
  
  public static SendMessageV2Response sendSms(SmsClient smsClient, String phone, SendMessageV2Request request) {
    request.setPhoneNumber(phone);
    return smsClient.sendMessage(request);
  }
}
